package re.jf.echangeur.utilitaire;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.math.BigDecimal;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

import re.fr.echangeur.xml.genere.EchangeurType;
import re.fr.echangeur.xml.genere.ObjectFactory;
import re.fr.echangeur.xml.genere.VideoType;

public class LectureEcritureXml {

  public static final String CONTEXTPATH = "re.fr.echangeur.xml.genere";
  public static final String NOMFICHER = "fichierVideo.xml";
  public static final String REPERTOIRE = "Echangeur";
  public static final String SEPARATEUR = System.getProperty("file.separator");

  public static VideoType creationVideoType(String nomDisque, String nomFichier, String propretaire) {
    VideoType videoType = new VideoType();
    videoType.setNomDisk(nomDisque);
    videoType.setNomFichier(nomFichier);
    videoType.setProprietaire(propretaire);
    return videoType;
  }

  public static void ecriture(EchangeurType echangeurType, String path) throws JAXBException, IOException {
    JAXBContext jc = JAXBContext.newInstance(CONTEXTPATH);
    echangeurType.getParametre()
      .setNombreFichier(new BigDecimal(echangeurType.getVideo()
        .size()));
    JAXBElement<EchangeurType> poElement = (new ObjectFactory()).createEchangeur(echangeurType);
    Marshaller m = jc.createMarshaller();
    m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    FileOutputStream fileOutputStream = new FileOutputStream(path, Boolean.FALSE);
    m.marshal(poElement, fileOutputStream);
    fileOutputStream.close();
  }

  public static String getChemin() {
    return REPERTOIRE + SEPARATEUR;
  }

  public static String getCheminFichier() {
    return getChemin() + NOMFICHER;
  }

  public static String getPropietaire(String path) throws FileNotFoundException, JAXBException {
    if (new File(path).exists()) {
      return lecture(path).getParametre()
        .getUtilisateur();
    } else {
      return "";
    }
  }

  public static EchangeurType lecture(File path) throws JAXBException, FileNotFoundException {
    JAXBContext jc = JAXBContext.newInstance(CONTEXTPATH);
    Unmarshaller u = jc.createUnmarshaller();
    JAXBElement< ? > poElement = (JAXBElement< ? >) u.unmarshal(new FileInputStream(path));
    EchangeurType po = (EchangeurType) poElement.getValue();
    return po;
  }

  public static EchangeurType lecture(String path) throws JAXBException, FileNotFoundException {
    JAXBContext jc = JAXBContext.newInstance(CONTEXTPATH);
    Unmarshaller u = jc.createUnmarshaller();
    JAXBElement< ? > poElement = (JAXBElement< ? >) u.unmarshal(new FileInputStream(path));
    EchangeurType po = (EchangeurType) poElement.getValue();
    return po;
  }

  public static void main(String[] args) {
    String xml = "H:\\Echangeur\\fichierVideo.xml";
    try {
      System.out.println(valicationXml(xml));
    } catch (JAXBException e) {
      e.printStackTrace();
    } catch (SAXException e) {
      e.printStackTrace();
    } catch (IOException e) {
      e.printStackTrace();
    }
  }

  public static boolean valicationXml(String path) throws JAXBException, SAXException, IOException {
    try {
      SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
      Schema schema = schemaFactory.newSchema(new File("src/schema/Echangeur.xsd"));
      Validator validator = schema.newValidator();
      validator.validate(new StreamSource(path));
      return true;
    } catch (SAXParseException e) {
      return false;
    }
  }
}
